var util = require("../../utils/util.js");
import wxRequest from '../../utils/wxRequest.js'
var app = getApp();
var timer;
var n = 1;
Page({
  data: {
    types: [{
      limit: 6,
      name: '简单',
      active: true
    }, {
      limit: 8,
      name: '一般'
    }, {
      limit: 10,
      name: '困难'
    }], //玩法类型
    userInfo: {},
    chance: true,
    awardsList: {},
    animationData: {},
    awards: {},
    btnDisabled: false,
    token: null,
    info: '', //中奖信息
    which: 2, //中奖id
    showModel: false, //成功显示的弹窗
    limit: 6, //默认等级:简单
    no_event: false, //设置开始事件无效，默认可点击
  },

  onReady: function(e) {},
  onLoad(options) {
    var that = this;
    wx.setBackgroundColor({
      backgroundColor: '#e44025',
    })
    that.initUserInfo(() => {
      that.getList();
    });
  },
  initUserInfo(cb) {
    var that = this;
    let userInfo = wx.getStorageSync('userInfo')
    let token = wx.getStorageSync('t')
    if (token) {
      app.globalData.userInfo = userInfo;
      that.setData({
        userInfo: app.globalData.userInfo,
        token: wx.getStorageSync('t')
      })
      cb && cb()
    } else {
      // 防止getUserInfo 在 Page.onLoad 之后返回以这种情况
      app.userInfoReadyCallback = res => {
        // console.log('userInfoReadyCallback', res)
        that.setData({
          userInfo: res.userInfo,
          token: wx.getStorageSync('t')
        })
        cb && cb()
      }
    }
  },
  getList(e) {
    wx.showToast({
      icon: "loading"
    })
    clearInterval(timer); //重置动画
    timer = null;
    this.setData({
      btnDisabled: false
    });
    if (e) {
      this.setData({
        limit: e.currentTarget.dataset.limit,
        info: ''
      });
      var index = e.currentTarget.dataset.index;
      for (var i in this.data.types) {
        var str = `types[${i}].active`;
        if (i == index) {
          this.setData({
            [str]: typeof(this.data.types[index]['active']) != 'unidefined' ? !this.data.types[index].active ? true : true : true
          });
        } else {
          this.setData({
            [str]: false
          });
        }
      }
    }
    wxRequest.getRequest('/zp/zp/parameters', {
      limit: this.data.limit
    }).then(res => { //获取列表
      //画抽奖圆盘
      var awards = res.data;
      var awardsList = [];
      var turnNum = 1 / awards.length; // 文字旋转 turn 值
      // 奖项列表
      for (var i = 0; i < awards.length; i++) {
        awardsList.push({
          turn: i * turnNum + 'turn',
          lineTurn: i * turnNum + turnNum / 2 + 'turn',
          award: awards[i].title
        });
      }
      this.setData({
        awardsList: awardsList,
        awards: res.data
      });
      wx.hideToast();
    })

  },
  bindGetUserInfo: function(e) {
    wxRequest.bindGetUserInfo(e);
  },
  //开始抽奖
  startFn() {
    console.log('333', this.data.no_event)
    var that = this;
    n = 1;
    this.setData({
      btnDisabled: true
    })
    timer = setInterval(function() {
      //开始旋转
      star.call(that);
      n++;
    }, 300);
    //启动动画
    function star() {
      var animation = wx.createAnimation({
        transformOrigin: "50% 50%",
        duration: 300,
        timingFunction: "linear"
      });
      animation.rotate(360 * n).step();
      this.setData({
        animationData: animation.export()
      })
    }
  },
  stopFn() { //停止抽奖
    if (!this.data.no_event) {
      var that = this;
      that.setData({
        no_event: true,
        btnDisabled: false
      }); //开始事件不生效
      wxRequest.getRequest('/zp/zp/stops', {
        limit: this.data.limit
      }).then(res => { //获取中奖下标
        let str = res.data.title == '2019' ? true : false;
        if (!str) { //不成功
          that.setData({
            no_event: false
          });
        } else {
          that.setData({
            no_event: true
          });
        }
        for (let i in that.data.awards) {
          if (that.data.awards[i].title == res.data.title) {
            that.setData({
              which: Number(i)
            });
            clearInterval(timer);
            timer = null;
            sto.call(that);

            function sto() {
              var animation = wx.createAnimation({
                transformOrigin: "50% 50%",
                duration: 200,
                timingFunction: "ease-out"
              });
              that.deg = 0;
              //旋转角度（每次转，从0deg开始,不计算上一次角度）
              that.deg = 360 * n + 360 - that.data.which * (360 / that.data.awards.length);
              animation.rotate(that.deg).step();
              that.setData({
                animationData: animation.export(),
              })
              // setTimeout(() => {
              that.setData({
                info: str ? '挑战成功' : that.data.awards[that.data.which].title == '2018' ? "还差一点点" : "挑战失败" + that.data.awards[that.data.which].title,
                showModel: str ? true : false
              })
              n = 1;
              // }, 200);
            }
          }
        }
      })
    } else {
      return false;
    }
  },
  closeFn() { //关闭中奖弹窗
    this.setData({
      showModel: false,
      no_event: false
    });
  },
  onShareAppMessage() {
    return {
      title: ' 挑战2019，你对得起你的双手吗？',
      path: '/pages/index/index'
    }
  },
  onHide() {
    clearInterval(timer); //重置动画
    timer = null;
    this.setData({
      btnDisabled: false
    });
  },
})